<!-- 月产量统计 -->
<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <div class="flex mt10" style="width: 100%; background-color: white">
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
        <Table
          :outTableHeight="10"
          :summaryData="summaryData"
          summary
          :tableHeader="tableHeader"
          :tableData="tableData"
          class="mt10"
          :isLoading="isLoading"
          @currentRow="currentRow"
        />
      </el-card>
    </div>
    <el-drawer v-model="drawer" :direction="'ttb'" size="small" :with-header="false">
      <span @click="drawer = false" style="float: right; cursor: pointer; color: rgb(7, 7, 7)">X</span>
      <div class="flex AIC" style="flex-direction: column">
        <div>
          <span style="text-align: center">{{ cutDown }}</span>
          <el-button class="ml10" type="primary" size="default" @click="exportExcel">导出</el-button>
        </div>
        <div id="main" style="width: 100%; height: 180px"></div>
        <div class="flex mt10 JCC" style="width: 100%">
          <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
            <div class="flex"></div>
            <JTable :isLoading="isLoading2" :tableHeader="tableHeader2" :tableData="tableData2" class="mt10" />
          </el-card>
        </div>
      </div>
    </el-drawer>
  </div>
</template>
<script lang="ts">
import { onMounted, ref } from 'vue'
import { watch } from 'vue'
import { debounce } from 'lodash'
import service from '@/utils/request'
import fileRequest from '@/utils/fileRequest'
import LoginWork from '@/utils/LoginWork'
import JMessage from '@/utils/JMessage'
import * as echarts from 'echarts'

export default {
  name: 'MonthlyOutputStatistics'
}
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
const tableHeader = ref([
  {
    width: 120,
    change: true,
    label: '月份/产量',
    prop: 'month'
  },
  {
    Multistage: true,
    label: '版房完成',
    children: [
      {
        label: '配色数',
        prop: 'qty1'
      },
      {
        blue: true,
        label: '双数',
        prop: 'qty2'
      }
    ]
  },
  {
    Multistage: true,
    label: '备料完成',
    children: [
      {
        label: '配色数',
        prop: 'qty3'
      },
      {
        blue: true,
        label: '双数',
        prop: 'qty4'
      }
    ]
  },
  {
    Multistage: true,
    label: '裁断完成',
    children: [
      {
        label: '配色数',
        prop: 'qty5'
      },
      {
        blue: true,
        label: '双数',
        prop: 'qty6'
      }
    ]
  },
  {
    Multistage: true,
    label: '电雕完成',
    children: [
      {
        label: '配色数',
        prop: 'qty7'
      },
      {
        blue: true,
        label: '双数',
        prop: 'qty8'
      }
    ]
  },
  {
    Multistage: true,
    label: '印刷完成',
    children: [
      {
        label: '配色数',
        prop: 'qty9'
      },
      {
        blue: true,
        label: '双数',
        prop: 'qty10'
      }
    ]
  },
  {
    Multistage: true,
    label: '无缝完成',
    children: [
      {
        label: '配色数',
        prop: 'qty11'
      },
      {
        blue: true,
        label: '双数',
        prop: 'qty12'
      }
    ]
  },
  {
    Multistage: true,
    label: '高频完成',
    children: [
      {
        label: '配色数',
        prop: 'qty13'
      },
      {
        blue: true,
        label: '双数',
        prop: 'qty14'
      }
    ]
  },
  {
    Multistage: true,
    label: '配套完成',
    children: [
      {
        label: '配色数',
        prop: 'qty15'
      },
      {
        blue: true,
        label: '双数',
        prop: 'qty16'
      }
    ]
  },
  {
    Multistage: true,
    label: '针车完成',
    children: [
      {
        label: '配色数',
        prop: 'qty17'
      },
      {
        blue: true,
        label: '双数',
        prop: 'qty18'
      }
    ]
  },
  {
    Multistage: true,
    label: '成型投料',
    children: [
      {
        label: '配色数',
        prop: 'qty19'
      },
      {
        blue: true,
        label: '双数',
        prop: 'qty20'
      }
    ]
  },
  {
    Multistage: true,
    label: '成型完成',
    children: [
      {
        label: '配色数',
        prop: 'qty21'
      },
      {
        blue: true,
        label: '双数',
        prop: 'qty22'
      }
    ]
  },
  {
    Multistage: true,
    label: '出货完成',
    children: [
      {
        label: '配色数',
        prop: 'qty23'
      },
      {
        blue: true,
        label: '双数',
        prop: 'qty24'
      }
    ]
  }
])
const getCurrentYearMonth = () => {
  const date = new Date()
  const year = date.getFullYear().toString() // 获取年份的后两位
  const month = (date.getMonth() + 1).toString().padStart(2, '0') // 获取月份并补零
  return `${year}`
}
const formList = ref([
  {
    dateType: 'year',
    type: 'dateMonth',
    dateLabel: '年份',
    dateValue: getCurrentYearMonth()
  },
  {
    type: 'button',
    checkboxLabel: '导出'
  },
  {
    type: 'button',
    checkboxLabel: '更新月产量'
  }
])
// 表一模块
const currentYear = () => {
  const date = new Date()
  const year = date.getFullYear().toString() // 获取年份的后两位
  return `${year}`
}
const currentMonth = () => {
  const date = new Date()
  const year = date.getFullYear().toString() // 获取年份的后两位
  const month = (date.getMonth() + 1).toString().padStart(2, '0') // 获取月份并补零
  return `${year}${month}`
}
// 表一按钮
const findClicked = async (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
  if (content === '更新月产量') {
    const res = (await fileRequest.get(
      `/GenerateProdReport/updateMonthProd?year=${currentYear()}&yearAndMonth=${currentMonth()}&companyId=${'BF'}`
    )) as any
    if (res.code === 200) {
      JMessage.success(res.message)
      getTableList()
    } else {
      JMessage.error(res.message)
    }
  }
  if (content === '导出') {
    const res = (await fileRequest.post(`/statements/downloadStitchMonthProd?month=${formList.value[0].dateValue}&prodSeq=${3}`)) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`utf-8''`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }
}
const tableData = ref([])
const isLoading = ref(true)
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData = ref<summaryDataType[]>([
  {
    index: 2,
    num: 0
  },
  {
    index: 3,
    num: 0
  },
  {
    index: 4,
    num: 0
  },
  {
    index: 5,
    num: 0
  }
])
const summaryData2 = ref<summaryDataType[]>([
  {
    index: 6,
    num: 0
  },
  {
    index: 7,
    num: 0
  },
  {
    index: 8,
    num: 0
  },
  {
    index: 9,
    num: 0
  },
  {
    index: 10,
    num: 0
  },
  {
    index: 11,
    num: 0
  },
  {
    index: 12,
    num: 0
  },
  {
    index: 13,
    num: 0
  },
  {
    index: 14,
    num: 0
  },
  {
    index: 15,
    num: 0
  },
  {
    index: 16,
    num: 0
  },
  {
    index: 17,
    num: 0
  },
  {
    index: 18,
    num: 0
  },
  {
    index: 19,
    num: 0
  },
  {
    index: 20,
    num: 0
  },
  {
    index: 21,
    num: 0
  },
  {
    index: 22,
    num: 0
  },
  {
    index: 23,
    num: 0
  },
  {
    index: 24,
    num: 0
  },
  {
    index: 25,
    num: 0
  },
  {
    index: 26,
    num: 0
  },
  {
    index: 27,
    num: 0
  },
  {
    index: 28,
    num: 0
  },
  {
    index: 29,
    num: 0
  },
  {
    index: 30,
    num: 0
  },
  {
    index: 31,
    num: 0
  },
  {
    index: 32,
    num: 0
  },
  {
    index: 33,
    num: 0
  },
  {
    index: 34,
    num: 0
  },
  {
    index: 35,
    num: 0
  },
  {
    index: 36,
    num: 0
  }
])
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}

// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.post<any, any>(
    `/statements/prodMonthProd?month=${
      formList.value.find((item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '年份')?.dateValue
    }`
  )
  const formattedData = [] as any
  if (tableDataList) {
    tableDataList.forEach((monthData: any) => {
      const obj: any = {
        month: monthData.month
      }
      monthData.list.forEach((item: any) => {
        // 根据工序名称设置对应的数量字段
        switch (item.wc) {
          case '板房完成':
            obj.qty1 = item.colorNum
            obj.qty2 = item.prodQty
            break
          case '备料完成':
            obj.qty3 = item.colorNum
            obj.qty4 = item.prodQty
            break
          case '裁断完成':
            obj.qty5 = item.colorNum
            obj.qty6 = item.prodQty
            break
          case '印刷完成':
            obj.qty7 = item.colorNum
            obj.qty8 = item.prodQty
            break
          case '无缝完成':
            obj.qty9 = item.colorNum
            obj.qty10 = item.prodQty
            break
          case '高频完成':
            obj.qty11 = item.colorNum
            obj.qty12 = item.prodQty
            break
          case '配套完成':
            obj.qty13 = item.colorNum
            obj.qty14 = item.prodQty
            break
          case '针车完成':
            obj.qty15 = item.colorNum
            obj.qty16 = item.prodQty
            break
          case '成型投料':
            obj.qty17 = item.colorNum
            obj.qty18 = item.prodQty
            break
          case '成型完成':
            obj.qty19 = item.colorNum
            obj.qty20 = item.prodQty
            break
          case '出货完成':
            obj.qty21 = item.colorNum
            obj.qty22 = item.prodQty
            break
          case '电雕完成':
            obj.qty23 = item.colorNum
            obj.qty24 = item.prodQty
            break
        }
      })
      formattedData.push(obj)
    })
  }
  isLoading.value = false
  tableData.value = formattedData.map((item: any) => {
    item.blue = true
    return item
  })
  total.value = tableDataList.total
  // currentRow()
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)
// 点击表给另一张表传数据
const currentTableRow = ref({}) as any
const choose = ref(0)
const currentProp = ref('')
const currentRow = async (row: any = tableData.value[0], scop: any = {}, item: any) => {
  currentTableRow.value = row
  drawer.value = true
  getTableList2(row, scop, item)
}
const drawer = ref(false)
const tableHeader9 = ref([
  {
    width: 120,
    label: '交期/产量',
    prop: 'prodTime'
  },
  {
    Multistage: true,
    label: '备料完成',
    children: [
      {
        label: '配色数',
        prop: 'qty19'
      },
      {
        label: '双数',
        prop: 'qty20'
      }
    ]
  }
])
const tableData9 = ref([])
const total2 = ref(0)
const handleSizeChange2 = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange2 = (val: number) => {
  currentPage.value = val
  getTableList()
}
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData9 = ref<summaryDataType[]>([
  {
    index: 8,
    num: 0
  }
])
const cutDown = ref('')
const isLoading2 = ref(false)
const tableData2 = ref([])
const tableHeader2 = ref([
  {
    width: 200,
    change: true,
    label: '日期/产量',
    prop: 'mon'
  },
  {
    Multistage: true,
    label: cutDown,
    children: [
      {
        width: 400,
        label: '配色数',
        prop: 'colorNum'
      },
      {
        width: 400,
        label: '新版数',
        prop: 'oldNum'
      },
      {
        width: 400,
        label: '改版数',
        prop: 'gbNum'
      }
    ]
  }
])
const getTableList2 = debounce(async (row: any, columns: any, item: any) => {
  let prodSeq = 0
  // 1-13，9不传，按照表头传递prodSeq
  if (columns.prop === 'qty1' || columns.prop === 'qty2') {
    prodSeq = 1
  } else if (columns.prop === 'qty3' || columns.prop === 'qty4') {
    prodSeq = 2
  } else if (columns.prop === 'qty5' || columns.prop === 'qty6') {
    prodSeq = 3
  } else if (columns.prop === 'qty7' || columns.prop === 'qty8') {
    prodSeq = 4
  } else if (columns.prop === 'qty9' || columns.prop === 'qty10') {
    prodSeq = 5
  } else if (columns.prop === 'qty11' || columns.prop === 'qty12') {
    prodSeq = 6
  } else if (columns.prop === 'qty13' || columns.prop === 'qty14') {
    prodSeq = 7
  } else if (columns.prop === 'qty15' || columns.prop === 'qty16') {
    prodSeq = 8
  } else if (columns.prop === 'qty17' || columns.prop === 'qty18') {
    prodSeq = 10
  } else if (columns.prop === 'qty19' || columns.prop === 'qty20') {
    prodSeq = 11
  } else if (columns.prop === 'qty21' || columns.prop === 'qty22') {
    prodSeq = 12
  } else if (columns.prop === 'qty23' || columns.prop === 'qty24') {
    prodSeq = 13
  }
  isLoading2.value = true
  // 将row.month的YYYYMM转换为YYYY-MM
  let month = row.month.split('').slice(0, 4).concat('-').concat(row.month.split('').slice(4, 6)).join('')
  let res = await LoginWork.post<any, any>(`/statements/prodMonthProdDetail?month=${month}&prodSeq=${prodSeq}`)
  if (res.code === 200) {
    cutDown.value = item.label
    tableData2.value = res.data.map((item: any) => {
      item.blue = true
      return item
    })
    isLoading2.value = false
    if (item.label !== '版房完成') {
      tableHeader2.value[1].children?.splice(1, tableHeader2.value[1].children.length, {
        width: 400,
        label: '双数',
        prop: 'prodQty'
      })
    }
    console.log(tableHeader2.value)
  } else {
    JMessage.error(res.message)
    isLoading2.value = false
    return
  }
  var chartDom = document.getElementById('main')
  var myChart = echarts.init(chartDom)
  var option

  option = {
    grid: {
      top: '10px',
      left: '0',
      right: '0',
      bottom: '20px',
      containLabel: true
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        crossStyle: {
          color: '#999'
        }
      }
    },
    xAxis: {
      type: 'category',
      data: tableData2.value.map((item: any) => item.mon)
    },
    legend: {
      bottom: '0',
      data: ['配色数', '双数']
    },
    yAxis: [
      {
        type: 'value'
      }
    ],
    series: [
      {
        name: '配色数',
        data: tableData2.value.map((item: any) => item.colorNum),
        type: 'bar'
      },
      {
        name: '双数',
        data: tableData2.value.map((item: any) => item.prodQty),
        type: 'bar'
      }
    ]
  }

  option && myChart.setOption(option)
}, 500)
const exportExcel = async () => {
  let month = formList.value[0].dateValue
  const res = (await fileRequest.post(
    `/statements/downloadStitchMonthProdTotal?choose=${choose.value}&month=${month}&prodSeq=${3}&workerId=${
      currentTableRow.value.workerId
    }&workerName=${currentTableRow.value.workerName}`
  )) as any
  const link = document.createElement('a') // 利用a标签自定义下载文件名
  let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
  link.style.display = 'none'
  //设置连接
  link.href = window.URL.createObjectURL(blob)
  const FileName = decodeURI(res.headers['content-disposition'].split(`utf-8''`)[1])
  link.download = FileName //自定义文件名字
  document.body.appendChild(link)
  //模拟点击事件
  link.click()
  document.body.removeChild(link)
}
</script>
